package com.woniuxy.test_map.my_treeset;

/**
 * description: TestTreeSet <br>
 * date: 2022/2/4 3:37 下午 <br>
 * author: shesaifei <br>
 * version: 1.0 <br>
 */

import java.util.Set;
import java.util.TreeSet;

/**
 * 测试TreeSet的使用
 */
public class TestTreeSet {
    public static void main(String[] args) {
        Set<Integer> set = new TreeSet<>();

        set.add(300);
        set.add(200);
        set.add(600);

        //按照元素递增的方式排好序
        for (Integer m : set) {
            System.out.println(m);
        }

        Set<Emp2> set2 = new TreeSet<>();
        set2.add(new Emp2(100, "zs", 3000));
        set2.add(new Emp2(50, "ls", 2000));
        set2.add(new Emp2(150, "汪汪", 8000));
        set2.add(new Emp2(60, "政治", 2000));

        for (Emp2 m2 : set2) {
            System.out.println(m2);
        }
    }

    static class Emp2 implements Comparable<Emp2> {

        int id;
        String name;
        double salary;

        public Emp2(int id, String name, double salary) {
            this.id = id;
            this.name = name;
            this.salary = salary;
        }

        @Override
        public String toString() {
            return "Emp2{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", salary=" + salary +
                    '}';
        }

        @Override
        public int compareTo(Emp2 o) {
            //负数：小于，0：等于，正数：大于
            if (this.salary > o.salary) {
                return 1;
            } else if (this.salary < o.salary) {
                return -1;
            } else {
                if (this.id > o.id) {
                    return 1;
                } else if (this.id < o.id) {
                    return -1;
                } else {
                    return 0;
                }
            }
        }
    }
}
